

import UIKit

class XWServerViewController: UIViewController {
    private var dataModel:XWServerModel!
    @IBOutlet weak var XWTableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        showUI()
        loadData()
    }
    deinit{
        NotificationCenter.default.removeObserver(self)
    }
    @IBAction func XWBackButtonClik(_ sender: UIButton) {
        dismiss(animated: true, completion: nil)
    }
}
extension XWServerViewController {
    private func loadData() {
        XWHttpTool.init().getNodeList { [self] model in
            dataModel = model
            dataModel.best_node?.ping = String(arc4random() % 50 + 10)
            dataModel.best_node?.isConnectVPN = false
            for tempModel in dataModel.nodes! {
                let number = arc4random() % 100 + 20
                tempModel.ping = String(number)
                tempModel.isConnectVPN = false
            }
            XWTableView.reloadData()
            if XWDataStateTool.shareManger.getIsConnectVPN() {
                printLog(XWDataStateTool.shareManger.getXWNodeInfoModel().node_id as Any)
                printLog(dataModel.best_node?.node_id as Any)
                if XWDataStateTool.shareManger.getXWNodeInfoModel().node_id == dataModel.best_node?.node_id && XWDataStateTool.shareManger.getSelectedIndexRow() == 0{
                    dataModel.best_node?.isConnectVPN = true
                }else{
                    for tempModel in dataModel.nodes! {
                        if XWDataStateTool.shareManger.getXWNodeInfoModel().node_id == tempModel.node_id{
                            tempModel.isConnectVPN = true
                        }
                    }
                }
                XWTableView.reloadData()
            }
        } failureHandler: { failure in
            self.showHint(failure)
        }

    }
}
extension XWServerViewController{
    private func showUI() {
        XWTableView.register(UINib.init(nibName: "XWServerTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
        XWTableView.separatorStyle = .none
        XWTableView.separatorColor = kClearColor
        XWTableView.backgroundColor = kClearColor
        XWTableView.showsVerticalScrollIndicator = false
        
    }
}
extension XWServerViewController:UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if dataModel == nil {
            return 0
        }else{
            if dataModel.best_node != nil && dataModel.nodes!.count > 0{
                return 2
            }else{
                return 0
            }
        }
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! XWServerTableViewCell
        cell.indexCellRow = indexPath.row
        cell.model = dataModel
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 0{
            return 150
        }else{
            return CGFloat(90 * (self.dataModel == nil ? 0 : dataModel.nodes!.count) + 60)
        }
        
    }
}
